<template>
  <div class="login-div">
    <div class="login-box">
      <div style="background-color: #dbecff;padding: 50px 20px;border-radius: 5px;box-shadow: 0 0 10px rgb(0,0,0,0.2)">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 350px">
          <div style="margin-bottom: 30px;font-size: 20px;font-weight: bold;text-align: center;">欢 迎 注 册</div>
          <el-form-item prop="username">
            <el-input size="lager" v-model="data.form.username" placeholder="请输入账号" prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="no">
            <el-input size="lager" v-model="data.form.no" placeholder="请输入工号" prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input size="lager" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock" show-password></el-input>
          </el-form-item>
          <el-form-item prop="ConfirmPassword">
            <el-input size="lager" v-model="data.form.ConfirmPassword" placeholder="请确认密码" prefix-icon="Lock" show-password></el-input>
          </el-form-item>
        </el-form>
        <div style="display: flex">
          <el-button type="primary" @click="register" size="large" style="width: 50%">注册</el-button>
          <el-button type="warning" @click="clear" size="large" style="width: 50%">清除</el-button>
        </div>
        <div style="margin-top: 10px;text-align: right">
          已有账号？请<a style="color: #73aee6;text-decoration: none" href="/login"> 登录</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref,reactive} from "vue";
import {User, Lock, Position} from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请确认密码'))
  }else {
    if (value !== data.form.password) {
      callback('两次密码不一致')
    }
    else{
      callback()
    }
  }
}

const data = reactive({
  form:{},
  rules: {
    username:[
      {required:true,message:'请输入账号',trigger:'blur'}
    ],
    no:[
      {required:true,message:'请输入工号',trigger:'blur'}
    ],
    password:[
      {required:true,message:'请输入密码',trigger:'blur'}
    ],
    ConfirmPassword:[
      {validator:validatePass,trigger:'blur'}
    ]
  }
})


const formRef= ref()
const clear = ()=>{
  data.form.username="";
  data.form.password="";
}

const register = ()=>{
  //校验通过的情况下，发送注册请求
  formRef.value.validate((valid)=>{
    if(valid){
      request.post('/register',data.form).then(res=>{
        if(res.code==='200'){
          ElMessage.success("注册成功");
          location.href='/login';
        }else{
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

</script>
<style scoped>
.login-div{
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/login.jpg");
  background-size: cover;
  background-position: 0 40px;

}
.login-box{
  position: absolute;
  height: 100%;
  display: flex;
  align-items: center;
  right:0;
  width: 50%;
}

</style>
